//计算利息方法
var paymethod = 1;
var isday = 0;
var borr_id = 0;
function Calculate(){ //w:总额，j：利率，C：周期
	w = document.getElementById("account").value;
	j = document.getElementById("apr").value;
	C = document.getElementById("time_limit").value;
	var money= 0.00;
	w = parseFloat(w);
	j = parseFloat(j);
	C = parseFloat(C);

	if ( paymethod == 2)//等额本息
	{
		money = CalculateOne(C,w,j);
	}
	else if( paymethod == 1)//按月付息，到期还款
	{
		money = CalculateFour(C,w,j);
	}
	else if(paymethod == 3)//到期一次性还本息
	{
	
		k = C;
		money = CalculateEight(k,w,j);
		money = money.toFixed(2)
	}
	alert('预计收益：'+money);
	//document.getElementById("money").value=money;
	//document.getElementById("tzi").href="/borrow/zhs_obj/"+borr_id;
	//document.getElementById("tzi").href="javascript:;";
};

function jisuan(id,j,isda,C,paymetho){ //j：利率，C：周期
	document.getElementById("account").value="";
	document.getElementById("money").value="";
	
	document.getElementById("apr").value=j+" %";
	if(isda == 1) ter = " 天";
	else ter = " 月";
	document.getElementById("time_limit").value=C+ter;
	paymethod = paymetho;
	isday = isda;
	borr_id = id;
};

function CalculateOne(C,w,j)
{
	var g = "";
	var v;//期次
	var B;//偿还本息
	var A;//偿还利息
	var z;//偿还本金
	var y;//剩余本金
	j = j / 1200;	
	var f = (w * C * j * Math.pow((1 + j), C)) / (Math.pow((1 + j), C) - 1) - w;//累计支付利息
	f = Math.round(f * 100) / 100;
	var s = f + w;
	s = Math.round(s * 100) / 100;//累计还款总额
	B = s / C;
	B = Math.round(B * 100) / 100;
	var sum_A = 0.0;
	for (v = 1; v <= C; v++)
	{
		A = (w * j * (Math.pow((1 + j), C) - Math.pow((1 + j), (v - 1)))) / (Math.pow((1 + j), C) - 1);
		
		A = Math.round(A * 100) / 100;
		z = B - A;
		z = Math.round(z * 100) / 100;
		y = (w * (Math.pow((1 + j), C) - Math.pow((1 + j), v))) / (Math.pow((1 + j), C) - 1);
		y = Math.round(y * 100) / 100;
		sum_A = sum_A + A;
	}
	return sum_A;
}

function CalculateFour(C,w,j)
{
	var g = "";
	var v;//期次
	var B;//每期偿还本息
	var A;//月偿还利息
	var z;//月偿还本金
	var y;//剩余本金
	j = j / 1200;	
	var f = w * C * j ;//累计支付利息
	f = Math.round(f * 100) / 100;
	var s = f + w;
	s = Math.round(s * 100) / 100;//累计还款总额
	
	B = w * j-w*j;
	B = Math.round(B * 100) / 100;
	var sum_A = 0;
	for (v = 1; v <= C; v++)
	{
		
		if(v==C){
			
		B=w+w*j*C;
		A = w*j*C;
		A = Math.round(A * 100) / 100;
		z = B - A;
		z = Math.round(z * 100) / 100;
		y = (w * (Math.pow((1 + j), C) - Math.pow((1 + j), v))) / (Math.pow((1 + j), C) - 1);
		y = Math.round(y * 100) / 100;
		sum_A = sum_A+A;
		}
		else{
		A = w * j-w*j;
		A = Math.round(A * 100) / 100;
		z = B - A;
		z = Math.round(z * 100) / 100;
		y = w;
		y = Math.round(y * 100) / 100;
		sum_A = sum_A+A;
		}
	}
	return sum_A;
}

function CalculateEight(C,w,j){
	var g = "";
	var v;//期次
	var B;//每期偿还本息
	var A;//月偿还利息
	var z;//月偿还本金
	var y;//剩余本金
	j = j / 1200;	
	var f = w * C * j ;//累计支付利息
	f = Math.round(f * 100) / 100;
	var s = f + w;
	s = Math.round(s * 100) / 100;//累计还款总额
	
	B = w * j;
	B = Math.round(B * 100) / 100;
	var sum_A = 0;
	for (v = 1; v <= C; v++)
	{
		if(v==C){
			B = w+w*j;
			B = Math.round(B * 100) / 100;
			A = w*j;
			A = Math.round(A * 100) / 100;
			z = B - A;
			z = Math.round(z * 100) / 100;
			y = 0;
			sum_A = sum_A+A;
		}
		else{
		A = w * j;
		A = Math.round(A * 100) / 100;
		z = B - A;
		z = Math.round(z * 100) / 100;
		y = w;
		y = Math.round(y * 100) / 100;
		sum_A = sum_A+A;
	}
	}
	return sum_A;
}


function GetUrlRelativePath()//获取相对路径
{
　　var url = document.location.toString();
　　var arrUrl = url.split("//");

　　var start = arrUrl[1].indexOf("/");
　　var relUrl = arrUrl[1].substring(start);//stop省略，截取从start开始到结尾的所有字符

　　if(relUrl.indexOf("?") != -1){
　　　　relUrl = relUrl.split("?")[0];
　　}
　　return relUrl;
}
//验证码
function changeImage() {
	document.getElementById("imgCode").src = "/yanzheng/index/?" + Math.random();
}